package com.pingan.generator.api.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.util.CollectionUtils;

import com.pingan.generator.api.annotation.ModaInput;
import com.pingan.generator.api.annotation.ModaOutput;
import com.pingan.generator.api.entity.FieldType;
import com.pingan.generator.api.entity.Param;
import com.pingan.generator.api.entity.User;

public class SelfAnnotationHandlerUtil {

	public static void updateGeneratorAPI(Class<?> clazz) {
		Field[] fields = clazz.getDeclaredFields();
		ArrayList<Param> list = new ArrayList<>();
		List<Param> firstList = null;
		for (Field obj : fields) {
			Class<?> type = obj.getType();
			// 判断是否为基本类型String,Integer,Double,Long
			System.out.println("数据类型：" + type);
			firstList = saveurl(obj, list);// 第一层输入输出
			boolean flag = jugeType(type);
			System.out.println("是否需要迭代：" + flag);
			if (flag) {
				System.out.println("可遍历或对象的类型：" + type);
				iteratIt(obj);
			}
		}
		System.out.println("可拿到的列表：" + JsonUtil.toJson(firstList));
	}

	/**
	 * 基本数据存入数据库
	 * 
	 * @param obj
	 * @author xuwei
	 * @date 2018年7月25日上午10:14:56
	 */
	private static List<Param> saveurl(Field obj, List<Param> list) {
		ModaOutput output = obj.getDeclaredAnnotation(ModaOutput.class);
		ModaInput input = obj.getDeclaredAnnotation(ModaInput.class);
		Class<?> type = obj.getType();
		if (output != null) {// 输入
			List<String> urlList = Arrays.asList(output.value());
			for (String url : urlList) {
				Param param = new Param();
				param.setUrl(url);
				param.setFieldName(obj.getName());
				param.setFieldType(FieldType.PLAIN);
				param.setSide(2);
				list.add(param);
			}
			System.out.println("||字段名称:" + obj.getName() + ",||字段类型：" + type + ",||字段入参：" + urlList + "===");
		}
		if (input != null) {// 输出
			List<String> urlList = Arrays.asList(input.value());
			for (String url : urlList) {
				Param param = new Param();
				param.setUrl(url);
				param.setFieldName(obj.getName());
				param.setFieldType(FieldType.PLAIN);
				param.setSide(2);
				list.add(param);
			}
			System.out.println("||字段名称:" + obj.getName() + ",||字段类型：" + type + ",||字段出参：" + urlList + "===");
		}

		if (!CollectionUtils.isEmpty(list)) {
			String json = JsonUtil.toJson(list);
			System.out.println("//////////////////////////////////");
			System.out.println("json值：" + json);
			System.out.println("==================================");
			return list;
		}
		return list;
	}

	/**
	 * 存入数据库
	 * 
	 * @param url
	 * @author xuwei
	 * @date 2018年7月25日上午10:31:59
	 */
	public void save(String url, int level, int parentLevel, String key, List<String> inputStrName, List<String> outputStrName, String authName) {
		System.out.println("数据保存中。。。");
	}

	public void update(String url, int level, String key, List<String> inputStrName, List<String> outputStrName, String updatePeople) {
		System.out.println("数据更新中。。。");
	}

	/**
	 * 
	 * @param obj
	 * @author xuwei
	 * @date 2018年7月25日上午9:56:06
	 */
	private static void iteratIt(Field obj) {
		System.out.println("&&&:" + obj.getName());
		System.out.println("递归方法运行中。。。");
	}

	/**
	 * 判断类型
	 * 
	 * @param type
	 * @author xuwei
	 * @date 2018年7月24日上午11:48:10
	 */
	public static boolean jugeType(Class<?> type) {
		// 为空看是否为基本数据类型
		String[] typeStr = { "int", "long", "byte", "short", "char", "float", "double", "boolean" };
		String[] typeUStr = { "Integer", "Long", "Byte", "Short", "Char", "Float", "Double", "Boolean" };
		List<String> strList = Arrays.asList(typeStr);// 基本数据类型
		List<String> ustrList = Arrays.asList(typeUStr);// 包装数据类型

		// 不是基本数据类型，不是包装数据类型
		/**
		 * object List<object> List<Map>
		 */
		if (strList.contains(type.getSimpleName()) || ustrList.contains(type.getSimpleName()) || "String".equals(type.getSimpleName())) {// 基本类型
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 
	 * { "name": "abc", "age": 18, "address": "中国", "versionList": [ {
	 * "updatedDate": "2018-07-25 09:52:38", "id": 11, "osType": "i", "appType":
	 * "bu", "version": "1.0" }, { "updatedDate": "2018-07-25 09:52:38", "id":
	 * 11, "osType": "i", "appType": "bu", "version": "1.0" } ], "sofa": {
	 * "name": "sssfoa", "length": 1.8, "width": 0.5 }
	 */
	public static void main(String args[]) {
		updateGeneratorAPI(User.class);
		// User user = new User();
		// user.setName("abc");
		// user.setAge(18);
		// user.setAddress("中国");
		//
		// AppVersionInfo appVersionInfo = new AppVersionInfo();
		// appVersionInfo.setId(11L);
		// appVersionInfo.setOsType("i");
		// appVersionInfo.setAppType("bu");
		// appVersionInfo.setVersion("1.0");
		// AppVersionInfo appVersionInfo2 = new AppVersionInfo();
		// appVersionInfo2.setId(11L);
		// appVersionInfo2.setOsType("i");
		// appVersionInfo2.setAppType("bu");
		// appVersionInfo2.setVersion("1.0");
		// ArrayList<AppVersionInfo> versionList = new ArrayList<>();
		// versionList.add(appVersionInfo);
		// versionList.add(appVersionInfo2);
		//
		// user.setVersionList(versionList);
		// Sofa sofa = new Sofa();
		// sofa.setName("sssfoa");
		// sofa.setLength(new BigDecimal("1.8"));
		// sofa.setWidth(new BigDecimal("0.5"));
		// user.setSofa(sofa);
		//
		// String json = JsonUtil.toJson(user);
		// System.out.println(json);
	}
}
